'use strict'

const { Controller } = require('egg')

class KujoMiva extends Controller {
  /**
  * @api {get} /KujoMiva 获取列表
  * @apiGroup KujoMiva
  */
  async index() {
    const { ctx } = this
    const { proid:id } = ctx.header

    console.log(id)

    const result = await this.app.databaseWuye.get('item', {id})

    ctx.returnBody(200, '进入了获取列表接口', result)
  }

  /**
  * @api {post} /KujoMiva 创建数据
  * @apiGroup KujoMiva
  */
  async create() {
    const { ctx } = this
    
    ctx.returnBody(200, '进入了创建数据接口')
  }

  /**
  * @api {put} /KujoMiva/:id 更改数据
  * @apiGroup KujoMiva
  */
  async update() {
    const { ctx } = this
    const { pro_id: id } = ctx.header

    await this.app.databaseWuye.beginTransactionScope(async conn => {
      const oldData = await conn.get('item', { id })
      const {
        commName = oldData.commName,
        introduce = oldData.introduce,
        service = oldData.service,
        icon = oldData.icon
      } = ctx.request.body
      const updateRow = {
        commName,
        introduce,
        service,
        icon
      }
      await conn.update('item', updateRow, { where: { id } })
    })
    ctx.returnBody(200, '物业信息修改成功')
  }

  /**
  * @api {delete} /KujoMiva/:id 删除数据
  * @apiGroup KujoMiva
  */
  async destroy() {
    const { ctx } = this

    ctx.returnBody(200, '进入了删除数据接口')
  }
}

module.exports = KujoMiva
